package com.hnevc.xiaoming.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hnevc.xiaoming.R;
import com.hnevc.xiaoming.bean.SpBean;

import java.util.List;

public class MySpAdapter extends BaseAdapter {

    private List<SpBean> list;
    private Context context;

    public MySpAdapter(List<SpBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView==null) {
            convertView = View.inflate(context, R.layout.sp_itme, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }
        viewHolder = (ViewHolder) convertView.getTag();
        SpBean data = list.get(position);
        viewHolder.name_itme.setText(data.getName());
        viewHolder.content_itme.setText(data.getContent());
        int img = context.getResources().getIdentifier(data.getImg(), "mipmap", context.getPackageName());
        viewHolder.img_itme.setImageResource(img);
        return convertView;
    }

    public static
    class ViewHolder {
        public View rootView;
        public ImageView img_itme;
        public TextView name_itme;
        public TextView content_itme;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.img_itme = (ImageView) rootView.findViewById(R.id.img_itme);
            this.name_itme = (TextView) rootView.findViewById(R.id.name_itme);
            this.content_itme = (TextView) rootView.findViewById(R.id.content_itme);
        }

    }
}
